import { http } from './request/index.js'

/*
	application/x-www-form-urlencoded;charset=utf-8
	multipart/form-data
	application/json
	text/xml
*/
const header = { 'content-type': 'application/json' }


export function wxLoginCode(data = {}, config = { header: {}, custom: {} }) {
	return http.post('/weixin/login', {}, {
		custom: { interfaceName: '用code换取openId等信息（/weixin/wechat）', ...config.custom },
		data
	})
}

export function updateUserInfo(params = {}, config = { header: {}, custom: {} }) {
	return http.put(`/weixin/wechat/${params.id}`, {}, {
		custom: { interfaceName: '修改用户信息（/weixin/wechat/{id}）', ...config.custom },
		data: params
	})
}

export function getUserInfo(params = {}, config = { header: {}, custom: {} }) {
	return http.get('/weixin/getWechatLoginUserInfo', {
		custom: { interfaceName: '获取用户信息（/weixin/getWechatLoginUserInfo）', ...config.custom },
		params
	})
}




export function getDept(params = {}, config = { header: {}, custom: {} }) {
	return http.get('/weixin/mini/depts', {
		custom: { interfaceName: '获取部门（/weixin/mini/depts）', ...config.custom },
		params
	})
}



export function getListVisit(params = {}, config = { header: {}, custom: {} }) {
	return http.get('/weixin/mini/myVisit', {
		custom: { interfaceName: '来访申请单列表（/weixin/mini/myVisit）', ...config.custom },
		params
	})
}
export function addPerson(params = {}, config = { header: {}, custom: {} }) {
	return http.post('/weixin/mini/person/add', {}, {
		custom: { interfaceName: '新增用户（/weixin/mini/person/add）', ...config.custom },
		data: params,
	})
}
export function addVisit(params = {}, config = { header: {}, custom: {} }) {
	return http.post('/weixin/mini/myVisit/add', {}, {
		custom: { interfaceName: '添加来访申请单（/weixin/mini/myVisit/add）', ...config.custom },
		data: params,
	})
}

export function editVisit(params = {}, config = { header: {}, custom: {} }) {
	return http.put(`/weixin/mini/myVisit/edit/${params.id}`, {}, {
		custom: { interfaceName: '修改来访申请单（/weixin/mini/myVisit/edit/{id}）', ...config.custom },
		data: params
	})
}

export function delVisit(params = {}, config = { header: {}, custom: {} }) {
	return http.delete('/weixin/mini/myVisit/remove', {}, {
		custom: { interfaceName: '删除来访申请单（/weixin/mini/myVisit/remove）', ...config.custom },
		params
	})
}

export function getInfoVisit(id = '', config = { header: {}, custom: {} }) {
	return http.get(`/weixin/mini/myVisit/${id}`, {
		custom: { interfaceName: '获取来访申请单（/weixin/mini/myVisit/{id}）', ...config.custom },
	})
}

export function editVisitStatus(params = {}, config = { header: {}, custom: {} }) {
	return http.put(`/weixin/mini/checkVisit/${params.id}`, {}, {
		custom: { interfaceName: '来访申请修改状态（/weixin/mini/checkVisit/{id}）', ...config.custom },
		data: params
	})
}





export function getNotices(params = {}, config = { header: {}, custom: {} }) {
	return http.get('/weixin/mini/noticList', {
		custom: { interfaceName: '公告列表（/weixin/mini/noticList）', ...config.custom },
		params
	})
}

export function getInfoNotices(noticeId = '', config = { header: {}, custom: {} }) {
	return http.get(`/weixin/mini/notic/${noticeId}`, {
		custom: { interfaceName: '公告详情（/weixin/mini/notic/{noticeId}）', ...config.custom },
	})
}



export function getCollect(params = {}, config = { header: {}, custom: {} }) {
	return http.get('/weixin/mini/myCollect', {
		custom: { interfaceName: '我的收藏列表（/weixin/mini/myCollect）', ...config.custom },
		params
	})
}

export function addCollect(params = {}, config = { header: {}, custom: {} }) {
	return http.post('/weixin/mini/myCollect/add', {}, {
		custom: { interfaceName: '新增收藏（/weixin/mini/myCollect/add）', ...config.custom },
		data: params,
	})
}

export function delCollect(params = {}, config = { header: {}, custom: {} }) {
	return http.delete('/weixin/mini/myCollect/remove', {}, {
		custom: { interfaceName: '删除收藏（/weixin/mini/myCollect/remove）', ...config.custom },
		params,
	})
}



export function getBrowhis(params = {}, config = { header: {}, custom: {} }) {
	return http.get('/weixin/mini/myBrowHis', {
		custom: { interfaceName: '我的浏览记录列表（/weixin/mini/myBrowHis）', ...config.custom },
		params
	})
}

export function addBrowhis(params = {}, config = { header: {}, custom: {} }) {
	return http.post('/weixin/mini/myBrowHis/addBrow', {}, {
		custom: { interfaceName: '新增浏览记录（/weixin/mini/myBrowHis/addBrow）', ...config.custom },
		data: params,
	})
}

export function deviceList(params = {}, config = { header: {}, custom: {} }) {
	return http.get('/weixin/mini/machine/getMachineList', {
		custom: { interfaceName: '设备列表（/weixin/mini/machine/getMachineList）', ...config.custom },
		params
	})
}

export function openDoor(params = {}, config = { header: {}, custom: {} }) {
	return http.get(`/weixin/mini/machine/openDoor/${params.machineId}`, {
		custom: { interfaceName: '设备开闸（/weixin/mini/machine/openDoor/{machineId}）', ...config.custom },
		params
	})
}


export function uploadFiles(url, tempFilePaths) {
	return new Promise((presolve, preject)=>{
		if ({}.toString.call(tempFilePaths) != '[object Array]') {
			throw new TypeError(`上传图片参数 tempFilePaths 类型错误!`)
		}
		//路径数组为空时  不上传
		if (tempFilePaths.length == 0) {
			presolve([])
			return
		}
		uni.showLoading({
			title: '上传图片中...',
			mask: truea
		});
		let uploads = []
		tempFilePaths.forEach((item,i) => {
			uploads[i] = new Promise ((resolve) => {
				uni.uploadFile({
					url: http.config.baseUrl + url,
					filePath: item,
					name: 'content', //这里根据自己的实际情况改
					formData: {},
					header: {},
					success(res) {
						resolve(JSON.parse(res.data))
					},
					fail(err) {
						uni.hideLoading()
					}
				})
			})
		})
		Promise.all(uploads).then(res => { // 等所有Promise请求完,自动拼成数组返回
			// 图片上传完成
			presolve(res)
			uni.hideLoading()
		}).catch(err => {
			preject(err)
			uni.hideLoading()
			uni.showToast({
				title: '上传失败请重试！',
				icon: 'none'
			})
		})
	})
}